package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishFlavorService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishFlavorServiceImpl implements DishFlavorService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加餐品及口味
     * @param dishDTO
     */
    @Override
    @Transactional( )
    public void disAndFlavorInsert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(0);
        dishMapper.dishInsert(dish);

        //数据库添加数据返回的id值
        Long dishId = dish.getId();
        List<DishFlavor> list = dishDTO.getFlavors();
        if(list != null && list.size() > 0) {
            list.forEach(item -> {
                item.setDishId(dishId);
            });
            flavorMapper.flovarInsert(list);
        }

    }

    /**
     * 菜品分页查询
     * @param
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //1.如果删除的菜品中有在销售的，就不能删除
        List<Dish> dish = dishMapper.getById(ids);
        dish.forEach(item -> {
            if(item.getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        });

        //2.判断是否可以删除，是否被套餐关联了

        List<Long> list =  setmealMapper.seleteByDishId(ids);
        if(list != null && list.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
//        //3.删除菜品表中的菜品数据
//        ids.forEach(id -> {
//            dishMapper.deleteById(id);
//            //4.删除菜品关联的口味数据
//            flavorMapper.deleteById(id);
//        });
        //3.删除菜品表中的菜品数据
            dishMapper.deleteById(ids);
            //4.删除菜品关联的口味数据
            flavorMapper.deleteById(ids);

    }

    /**
     * 根据id插叙菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //查询菜品信息
        Dish dish = dishMapper.getByOneId(id);
        //查询口味信息
        List<DishFlavor> list = flavorMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    @Transactional()
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //1.更新菜品信息
        dishMapper.update(dish);
        //2.删除口味信息
        flavorMapper.deleteByOneId(dishDTO.getId());
        //3.添加口味信息
        List<DishFlavor> list = dishDTO.getFlavors();
        list.forEach(it -> {
            it.setDishId(dishDTO.getId());
        });
        if (dishDTO.getFlavors() != null && dishDTO.getFlavors().size() > 0) {
            flavorMapper.flovarInsert(dishDTO.getFlavors());
        }
    }
    /**
     * 修改菜品起售停售状态
     */
    @Override
    public void setStatus(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
       dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listGetById(Long categoryId) {
        List<Dish> list = dishMapper.listGetById(categoryId);
        return list;

    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listGetById(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
